package com.imooc.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wt on 2017/10/27.
 */
@Aspect
@Component
public class HttpAspect {
    private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class);

    /**
     * 定义一个公用的方法 log()
     */
    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //url
        LOGGER.info("url = {}",request.getRequestURL());

        //method
        LOGGER.info("method = {}",request.getMethod());

        //ip
        LOGGER.info("ip = {}",request.getRemoteAddr());

        //类方法
        LOGGER.info("class_method = {}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());

        //参数
        LOGGER.info("args = {}",joinPoint.getArgs());
    }

    /**
     * 在http请求到GirlController 中的任何方法 之后 打印"dswxh"
     */
    @After("log()")
    public void doAfter() {
        LOGGER.info("dswxh");
    }

    /**
     * 获取方法返回内容
     * @param object
     */
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object) {
        LOGGER.info("response = {}",object.toString());
    }
}
